<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 主体区域 -->
        <el-container>
            <!-- 左侧导航 -->
            <el-aside :width="isCollapse ? '64px':'200px'">
                <el-menu unique-opened class="el-menu-vertical-demo"
                     active-text-color="#409bef" :collapse="isCollapse" :collapse-transition="false" background-color="#333744" text-color="#fff" :default-active="activePath" router >
                    <!-- 展开隐藏按钮 -->
                    <div class="toggle-button" @click="toggleCollapse ">{{isCollapse ? '展开列表':'隐藏列表'}}</div>
                    <!-- <div class="toggle-button" @click="toggleCollapse ">|||</div> -->
                    <!-- 一级 -->
                    <el-submenu :index="item.id + ''" v-for="item in meunlist" :key="item.id">
                        <template slot="title">
                            <i :class="iconsObj[item.id]" class="margin-right"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级 -->
                        <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
                            <i class="el-icon-menu"></i>
                            <span>{{subitem.authName}}</span>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  created () {
    this.getMeunList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      // 左侧列表数据
      meunlist: [],
      // 左侧列表 icon
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 展开隐藏
      isCollapse: false,
      // 激活菜单的 index
      activePath: ''
    }
  },
  methods: {
    logout: function () {
    //   window.sessionStorage.removeItem('token')
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧列表数据
    async getMeunList () {
      let { data: result } = await this.$http.get('menus')
      // console.log(result)
      if (result.meta.status !== 200) return this.$message.error(result.meta.msg)
      this.meunlist = result.data
    },
    // 左侧列表展开和隐藏
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 存储激活菜单的 index
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
    height: 100%;
}

.el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373d41;
    font-size: 20px;
    color: #fff;

    >div {
        display: flex;
        align-items: center;

        span {
            margin-left: 10px;
        }
    }
}

.el-aside {
    background-color: #333744;
    .el-menu{
        border-right: none;
    }
}

.el-main {
    background-color: #eaedf1;
}

.margin-right{
    margin-right: 10px;
}

.toggle-button{
    background-color: #4a5064;
    text-align: center;
    line-height: 26px;
    font-size: 10px;
    color: #fff;
    letter-spacing: .2em;
    cursor: pointer;
}
</style>
